package com.ops.service.ns.system.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSLookUps;
import com.ops.schema.ns.system.OPSMenu;
import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.OPSUserAction;
import com.ops.schema.ns.system.RetrieveMenuRequest;
import com.ops.schema.ns.system.RetrieveMenuResponse;
import com.ops.schema.ns.system.SearchLookUpsRequest;
import com.ops.schema.ns.system.SearchLookUpsResponse;
import com.ops.schema.ns.system.SearchUserActionsRequest;
import com.ops.schema.ns.system.SearchUserActionsResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;

public class UserActionMBean {

	private String action;
    private String user;
    private String objectType; 
    private String actionFromDate;
    private String actionToDate;
	private List<OPSUserAction> opsUserActionlst;
	private List<SelectItem> actionList;
	private List<SelectItem> objectList;
    
    
    public void searchUserActions() {
		System.out.println("Inside searchRules");
		SearchUserActionsRequest userActionsRequest = new SearchUserActionsRequest();
		userActionsRequest.setAction(action);
		userActionsRequest.setObjectType(objectType);
		userActionsRequest.setUser(user);
		userActionsRequest.setActionFromDate(actionFromDate);
		userActionsRequest.setActionToDate(actionToDate);
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		uditInfo.setAction("S");
		uditInfo.setObjectType(getMenuId());
		userActionsRequest.setOPSRecordAuditInfo(uditInfo);
		try {
			SearchUserActionsResponse actionsResponse = OnlinePayrollSystemServiceClient
					.searchUserActions(userActionsRequest);
			setOpsUserActionlst(actionsResponse.getOPSUserAction());

		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void resetuserActions() {
		 setAction(null);
		 setUser(null);
		 setObjectType(null); 
		 setActionFromDate(null);
		 setActionToDate(null);
	}
	
	
	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext()
				.getSession(false);
		return session.getId();
	}

	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}
	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}
	private String getRoleId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("RoleId").toString();
	}
    
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getObjectType() {
		return objectType;
	}
	public void setObjectType(String objectType) {
		this.objectType = objectType;
	}
	public String getActionFromDate() {
		return actionFromDate;
	}
	public void setActionFromDate(String actionFromDate) {
		this.actionFromDate = actionFromDate;
	}
	public String getActionToDate() {
		return actionToDate;
	}
	public void setActionToDate(String actionToDate) {
		this.actionToDate = actionToDate;
	}

	public List<OPSUserAction> getOpsUserActionlst() {
		return opsUserActionlst;
	}

	public void setOpsUserActionlst(List<OPSUserAction> opsUserActionlst) {
		this.opsUserActionlst = opsUserActionlst;
	}

	public List<SelectItem> getActionList() {
		if (actionList == null) {
			actionList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			actionList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			masterRequest.setType("USER-ACTION");
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					actionList.add(item);
				}
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}

		return actionList;
	}

	public void setActionList(List<SelectItem> actionList) {
		this.actionList = actionList;
	}

	public List<SelectItem> getObjectList() {
		if (objectList == null) {
			objectList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			objectList.add(defitem);
			RetrieveMenuRequest masterRequest = new RetrieveMenuRequest();
			masterRequest.setRoleId(getRoleId());
			masterRequest.setUserId(getuserId());
			try {
				RetrieveMenuResponse masterResponse = OnlinePayrollSystemServiceClient
						.retriveMenu(masterRequest);
				for (OPSMenu master : masterResponse.getOPSMenu()) {
					if(master.getParentMenuId() == null)
						{
							SelectItem item = new SelectItem(master.getId(),
									master.getName());
							objectList.add(item);
						}
				}
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return objectList;
	}

	public void setObjectList(List<SelectItem> objectList) {
		this.objectList = objectList;
	}

	
}
